package com.example.tvrecyclerviewkotlin.adapter

import android.content.Context
import android.os.Build
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.Nullable
import androidx.core.view.ViewCompat
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.example.tvrecyclerviewkotlin.R
import com.example.tvrecyclerviewkotlin.bean.GameListBean
import com.example.tvrecyclerviewkotlin.util.GlideUtils


/**
 *@author: njb
 *@date:  2021/2/1 13:03
 *@desc:
 */
abstract class GameListAdapter(
    private val mList: List<GameListBean>?,
    private val context: Context?,
    param: OnItemClickListener
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private var mOnFocusChangeListener: OnItemFocusChangeListener? = null
    private var mOnItemClickListener: OnItemClickListener? = null
    private var mLayoutInflater = LayoutInflater.from(context)

    interface OnItemClickListener {
        fun onItemClick(view: View?, position: Int)
    }

    interface OnItemFocusChangeListener {
        fun onItemFocusChange(
            view: View?,
            position: Int,
            hasFocus: Boolean
        )
    }


    fun setOnItemClickListener(@Nullable listener: OnItemClickListener?) {
        mOnItemClickListener = listener
    }

    fun setOnFocusChangeListener(@Nullable mOnFocusChangeListener: OnItemFocusChangeListener?) {
        this.mOnFocusChangeListener = mOnFocusChangeListener
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val v: View = mLayoutInflater!!.inflate(R.layout.listview_item, parent, false)
        return RecyclerViewHolder(v)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val viewHolder = holder as RecyclerViewHolder
        viewHolder.tv.text = mList!![position].gameName
        GlideUtils.loadImg(context, mList!![position].img, viewHolder.iv)
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener { v ->
                mOnItemClickListener!!.onItemClick(
                    v,
                    position
                )
            }
        }
        holder.itemView.isFocusable = true
        holder.itemView.setOnFocusChangeListener { v, hasFocus ->
            if (hasFocus) {
                focusStatus(v, holder.getAdapterPosition())
            } else {
                normalStatus(v)
            }
        }
        holder.itemView.setOnHoverListener(object : View.OnHoverListener {
            override fun onHover(v: View, event: MotionEvent): Boolean {
                val what = event.action
                when (what) {
                    MotionEvent.ACTION_HOVER_ENTER -> {
                        val recyclerView = holder.itemView.parent as RecyclerView
                        val location = IntArray(2)
                        recyclerView.getLocationOnScreen(location)
                        val x = location[0]
                        //                            LogUtil.i("swj","GalleryAdapter.onHover.x="+x +",width = "+(recyclerView.getWidth()+x));
                        //为了防止滚动冲突，在滚动时候，获取焦点为了显示全，会回滚，这样会导致滚动停止
                        if (recyclerView.scrollState == RecyclerView.SCROLL_STATE_IDLE) {
                            //当超出RecyclerView的边缘时不去响应滚动
                            if (event.rawX > recyclerView.width + x || event.rawX < x) {
                                return true
                            }
                            //鼠标进入view，争取到焦点
                            v.requestFocusFromTouch()
                            v.requestFocus()
                            //                                LogUtil.i(this,"HomeTvAdapter.onHover.position:"+position);
                            focusStatus(v, holder.getAdapterPosition())
                        }
                    }
                    MotionEvent.ACTION_HOVER_MOVE -> {
                    }
                    MotionEvent.ACTION_HOVER_EXIT -> normalStatus(v)
                }
                return false
            }
        })
    }

    override fun getItemCount(): Int {
        return mList!!.size
    }

    private class RecyclerViewHolder internal constructor(itemView: View) : ViewHolder(itemView) {
        var tv: TextView = itemView.findViewById(R.id.tv_name)
        var iv: ImageView = itemView.findViewById(R.id.iv_bg) as ImageView

    }

    /**
     * item获得焦点时调用
     *
     * @param itemView view
     */
    private fun focusStatus(itemView: View?, position: Int) {
        if (itemView == null) {
            return
        }
        val scal: Float
        scal = 1.8f
        if (Build.VERSION.SDK_INT >= 21) {
            //抬高Z轴
            ViewCompat.animate(itemView).scaleX(scal).scaleY(scal).translationZ(1.1f).start()
        } else {
            ViewCompat.animate(itemView).scaleX(scal).scaleY(scal).start()
            val parent = itemView.parent as ViewGroup
            parent.requestLayout()
            parent.invalidate()
        }
        onItemFocus(itemView)
    }

    /**
     * 当item获得焦点时处理
     *
     * @param itemView itemView
     */
    protected abstract fun onItemFocus(itemView: View?)

    /**
     * item失去焦点时
     *
     * @param itemView item对应的View
     */
    private fun normalStatus(itemView: View?) {
        if (itemView == null) {
            return
        }
        if (Build.VERSION.SDK_INT >= 21) {
            ViewCompat.animate(itemView).scaleX(1.0f).scaleY(1.0f).translationZ(0f).start()
        } else {
            ViewCompat.animate(itemView).scaleX(1.0f).scaleY(1.0f).start()
            val parent = itemView.parent as ViewGroup
            parent.requestLayout()
            parent.invalidate()
        }
        onItemGetNormal(itemView)
    }

    /**
     * 当条目失去焦点时调用
     *
     * @param itemView 条目对应的View
     */
    protected abstract fun onItemGetNormal(itemView: View?)
}